import { Component, OnInit, Inject } from '@angular/core';
import { _HttpClient } from '@delon/theme';
import {UtilityService} from '../../../../service/utils.service';
import {appConfig} from '../../../../service/common';
import {NzModalService, NzNotificationService} from 'ng-zorro-antd';
import {Router} from '@angular/router';
import { DA_SERVICE_TOKEN, ITokenService } from '@delon/auth';
import * as _ from 'lodash';
import * as moment from 'moment';
@Component({
  selector: 'app-who-change',
  templateUrl: './who-change.component.html',
  styleUrls: ['./who-change.component.less'],
})
export class WhoChangeComponent implements OnInit {

    constructor(
        private http: _HttpClient,
        private router: Router,
        private utilityService: UtilityService,
        private modal: NzModalService,
        private nznot: NzNotificationService,
        @Inject(DA_SERVICE_TOKEN) private tokenService: ITokenService,
    ) { }
  ngOnInit() {
     
      this.getworkData();
      this.getprofiles();
    //  this.ListDataTotal = this.ListData.length;
  
  }
   tabs = [
    {index: 1, title:'工作项'},
    {index: 2, title:'代码'},
    {index: 3, title:'环境'},
  ];
      searchworks = []
      Workitem:number;
      WorkitemLbel = "总计";
      PlaceHolder ='请选择工作项';
      SelectedIndex = 0;
      ListDataTotal:number;
      profiles:number;
      fileName:string;
     WorksArr = [];
     profileArr = [];
     singleValue = false;
     getworkData() {
        this.searchworks = []
        this.utilityService.getData(appConfig.testUrl  + appConfig.API.sWorkitem)
            .subscribe(
                (val) => {
                  val.result.forEach(i=>{
                      
                     this.WorksArr.push({label:i.itemName,value:i.guid})
                  })
                  this.searchworks = this.WorksArr;
                    if (val.result.length === 0) {
                        this.nznot.create('error', '暂无工作项' , '没有分配的工作项');
                    }
                },(error)=>{
                        this.nznot.create('error', error.code,  error.msg);
                }
            );
    }

    getprofiles(){
  this.profileArr = []
      this.utilityService.getData(appConfig.testUrl  +"/sProfiles/listAll")
            .subscribe(
                (val) => {

                 val.result.forEach(i=>{
                     this.profileArr.push({label:i.profilesName,value:i.guid})
                  })
                     this.msg = '按运行环境筛选';
         
             this.selectLabel = this.profileArr
             
                },(error)=>{
                        this.nznot.create('error', error.code,  error.msg);
                }
            );
    }
  ListData =[
     
  ];

  ListHeaderDate = [
      { label: '序号', value: 'index', isclick: false, title: true },
      { label: '代码', value: 'fullPath', isclick: false, title: true },    
  ];
  childListData = [];
  childListHeaderDate =[
      { label: '工作项', value: 'workitem', isclick: false, title: true },
      { label: '环境', value: 'profiles', isclick: false, title: true }, 
      { label: '操作人', value: 'deliver', isclick: false, title: true },
      { label: '时间', value: 'deliveryTime', isclick: false, title: true }, 
      
  ]
  childListDataTotal:number;
  listloading = false;

  checkWorkitem(it,index){
   if(!it){
       return;
   }
    this.searchworks.forEach(i=>{ 
      if(i.value == it){
          this.WorkitemLbel = i.label  
      }
    })
    let obj:any;
     let flag = "0";
    if(this.singleValue){
          flag = "1";
    }
  
    switch(index){
           case 1 :
             obj = {
                 'guidWorkItem':it,
                 'selectFlag':flag
               }
               this.WorkitemLbel = "工作项[ "+this.WorkitemLbel+" ]下总计代码"
             break;
             case 2 :
          obj = {
               'fileName':it,
               'selectFlag':flag
            }
              this.WorkitemLbel = "文件名[ "+this.fileName+" ]下总计代码"
             break;
             case 3 :
        obj = {
                 'guidProfiles':it,
                 'selectFlag':flag
             }
               this.WorkitemLbel = "环境[ "+this.WorkitemLbel+" ]下总计代码"
             break;
    }

  
    this.listloading = true;
    this.utilityService.postData(appConfig.testUrl  +  '/sDeliveryList/standardList/compare',  obj )
            .subscribe(
                (val) => {
                this.childListData = [];
                this.listloading = false;
                val.result.forEach((i,it)=>{
                    i.index = it+1
                    i.list.forEach(element => {
                              element.profiles = element.guidProfiles.target
                          element.workitem = element.guidWorkitem.target
                        element.deliveryTime = moment(element.deliveryTime).format('YYYY-MM-DD');
                    });
                })         
                this.ListData = val.result
               },(error)=>{
                        this.listloading = false;
                         this.nznot.create('error', error.code,  error.msg);
                })
 
  }
  selectLabel = [];
  changeTab(it){
      this.SelectedIndex = it;
      this.Workitem = null;
      console.log(it);
       switch(this.SelectedIndex){
             case 0 :
             this.PlaceHolder = '请选择工作项';
               this.msg = '按运行环境筛选';
             this.searchworks = this.WorksArr;
             this.selectLabel = this.profileArr
             break;
             case 1 :
             this.PlaceHolder = '请输入代码路径'
             break;
             case 2 :
             this.searchworks = this.profileArr;
              this.selectLabel = this.WorksArr
             this.PlaceHolder = '请选择环境'
               this.msg = '按工作项筛选'
             break;
       }
       
  }
  childs = []
  ClickTrbacground(it){
     console.log( it)
      this.ListData.forEach(i=>{
           i.isactive = false;
          if(it.guid === i.guid ){
               i.isactive = true;
               this.childListData = it.list
               this.childs = it.list
          }
      })
         
  }
  selectedRow(it){
  
  }
  searchtab:any;
  msg:string
  selected(it,index){
     if(!it ){
         this.childListData =   this.childs;
         return;
     }
     let arr = [];
       switch(index){
             case 1 :
             this.childs.forEach(i=>{
                if(i.profiles == it){
                    arr.push(i)
                }
             })
         
             break;
             case 3 :
              this.childs.forEach(i=>{
                if(i.workitem == it){
                    arr.push(i)
                }
             })
           
             break;
       }

        if(arr.length > 0){
                    this.childListData = arr
            }else{
                this.searchtab = null;
                this.nznot.create('error','暂无分类', '');
            }
             console.log(arr)
   

  }
  buttonEvent(it){

  }
}